import { createRouter, createWebHistory } from 'vue-router'

  const constantRoutes = [
      {
          path: 'index',
          name: 'index',
          component: () => import('@/views/index/index.vue')
        },
      {
        path: '/',
        name: 'login',
        component: () => import('@/views/login/index.vue')
      }
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes // 初始只有固定路由
})
/**
 * 登录校验（全局路由守卫）
 */
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const whiteList = ['/'] // 白名单：登录页

  if (!token && !whiteList.includes(to.path)) {
    // 没有 token 且访问的不是白名单页面 -> 跳转到登录页
    next('/')
  } else {
    // 放行
    next()
  }
})

export default router
